package com.example.administrator.jiangsu2021_3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.util.Map;

import cn.com.newland.nle_sdk.requestEntity.SignIn;
import cn.com.newland.nle_sdk.responseEntity.SensorInfo;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;
import retrofit2.Call;

public class MainActivity extends AppCompatActivity {
    private NetWorkBusiness netWorkBusiness;
    private double peoplel;
    private double light,light_max;
    private Boolean flag=true;
    private Button btn_setting;
    private ToggleButton toggleButton;
    private TextView tv_light;
    private static Thread myThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        btn_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, setting.class);
                startActivity(intent);
            }
        });
        //final Mythread mythread=new Mythread();
        //mythread.start();
        myThread = new Mythread();
        myThread.start();
        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(toggleButton.isChecked()){
                    flag=false;
                    control("95498","z_light",1);
                    Log.i("main","1");
                }else {
                    control("95498","z_light",0);
                    Log.i("main","0");
                    flag=true;
                    myThread = new Mythread();
                    myThread.start();
                }
            }
        });

    }

    private void init(){

        light_max=50.0;
        toggleButton=findViewById(R.id.toggleButton);
        btn_setting=findViewById(R.id.btn_setting);
        tv_light=findViewById(R.id.tv_light);
        String acc = SaveandGet.getAcc(MainActivity.this);
        netWorkBusiness=new NetWorkBusiness(acc,"http://api.nlecloud.com:80");
    }
    private void  getpepole(){
        netWorkBusiness.getSensor("95498", "z_body", new NCallBack<BaseResponseEntity<SensorInfo>>(this) {
            @Override
            protected void onResponse(BaseResponseEntity<SensorInfo> response) {
                if(response!=null){
                    if(response.getStatusCode()==0){
                        SensorInfo sensorInfo = response.getResultObj();
                        peoplel = Double.parseDouble(sensorInfo.getValue());
                    }
                }
            }
        });
    }
    private void getlight(){
        netWorkBusiness.getSensor("95498", "z_light2", new NCallBack<BaseResponseEntity<SensorInfo>>(this) {
            @Override
            protected void onResponse(BaseResponseEntity<SensorInfo> response) {
                if(response!=null){
                    if(response.getStatusCode()==0){
                        SensorInfo sensorInfo = response.getResultObj();
                        light = Double.parseDouble(sensorInfo.getValue());
                    }
                }
            }
        });
    }
    private void control(String id, final String tagapi, Object value){
        netWorkBusiness.control(id,tagapi,value,new NCallBack<BaseResponseEntity>(this) {
            @Override
            protected void onResponse(BaseResponseEntity response) {
                if(response!=null){
                    Log.i("main",tagapi);
                }else {
                    Toast.makeText(MainActivity.this,"请求内容为空",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private class Mythread extends  Thread{
        @Override
        public void run() {
            super.run();
            while (flag){
                getlight();
                getpepole();
                Map<String, String> map = SaveandGet.get(MainActivity.this);
                if(map.get("lightmax")!=null){
                    light_max = Double.parseDouble(map.get("lightmax"));
                }
                if(peoplel==1&&light<light_max){
                    control("95498","z_light",1);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_light.setText("开灯");
                        }
                    });
                    Log.i("main","开灯");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    control("95498","z_light",0);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tv_light.setText("关灯");
                        }
                    });
                    Log.i("main","关灯");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
